﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WisdriContacts.Resources;
using Microsoft.Phone.Tasks;
using System.Windows.Media;

namespace WisdriContacts
{
    using System.IO.IsolatedStorage;

    public partial class DetailsPage : PhoneApplicationPage
    {
        // 构造函数
        public DetailsPage()
        {
            InitializeComponent();

            // 用于本地化 ApplicationBar 的示例代码
            //BuildLocalizedApplicationBar();
        }

        // 导航页面以将数据上下文设置为列表中的所选项时
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            if (DataContext == null)
            {
                string selectedEmpno = "";
                if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedEmpno))
                {
                    DataContext = App.ViewModel.Items.Single(p => p.ID == selectedEmpno);
                }
            }

            IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

            bool isAdmin = false;
            if (settings.TryGetValue("IsAdmin", out isAdmin))
            {
                if (isAdmin)
                {
                    CarInfoPanel.Visibility = Visibility.Visible;
                }
            }


        }

        private void Dial_Tap(object sender, RoutedEventArgs e)
        {
            try
            {
                var phoneCallTask = new PhoneCallTask();

                phoneCallTask.PhoneNumber = (sender as TextBlock).Text.ToString().Trim();
                phoneCallTask.DisplayName = textBlockName.Text.Trim();

                phoneCallTask.Show();
            }
            catch (Exception ex)
            {
                ShellToast t = new ShellToast();
                t.Content = ex.Message;
                t.Show();
            }
        }

        private void TextDepartment_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            if (string.IsNullOrEmpty(TextDepartment.Text))
                return;

            App.ViewModel.SubTitle = TextDepartment.Text;

            // 导航到新页面
            NavigationService.Navigate(new Uri("/ContactListPage.xaml?selectedItem=" + TextDepartment.Text,
                UriKind.Relative));
        }

        private void Email_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            try
            {
                var emailComposeTask = new EmailComposeTask();

                emailComposeTask.Subject = "主题";
                emailComposeTask.Body = string.Empty;
                emailComposeTask.To = TextId.Text.Trim() + "@wisdri.com";
                emailComposeTask.Cc = string.Empty;
                emailComposeTask.Bcc = string.Empty;

                emailComposeTask.Show();
            }
            catch (Exception ex)
            {
                ShellToast t = new ShellToast();
                t.Content = ex.Message;
                t.Show();
            }
        }

        private Color systemTextColor;

        private void MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            systemTextColor = ((SolidColorBrush)((TextBlock)sender).Foreground).Color;
            ((TextBlock)sender).Foreground = new SolidColorBrush(Colors.Red);
        }

        private void MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            ((TextBlock)sender).Foreground = new SolidColorBrush(systemTextColor);
        }

        private void TextHold(object sender, System.Windows.Input.GestureEventArgs e)
        {
            Clipboard.SetText(((TextBlock)sender).Text);
            MessageBox.Show("已复制");
        }

        // 用于生成本地化 ApplicationBar 的示例代码
        //private void BuildLocalizedApplicationBar()
        //{
        //    // 将页面的 ApplicationBar 设置为 ApplicationBar 的新实例。
        //    ApplicationBar = new ApplicationBar();

        //    // 创建新按钮并将文本值设置为 AppResources 中的本地化字符串。
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // 使用 AppResources 中的本地化字符串创建新菜单项。
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}